package com.eurasia.assignment.infrastructure.hoj;

import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.eurasia.assignment.domain.hoj.HOJFullCaseJudgement;
import com.eurasia.assignment.domain.hoj.HOJJudgementStatus;
import com.eurasia.assignment.service.port.out.HOJFullCaseJudgementPersistencePort;

import lombok.RequiredArgsConstructor;

/**
 * 完整测试用例判题记录持久化适配器
 */
@Component
@RequiredArgsConstructor
public class HOJFullCaseJudgementPersistenceAdapter implements HOJFullCaseJudgementPersistencePort {

    private final HOJFullCaseJudgementRepository repository;
    private final HOJFullCaseJudgementEntityMapper mapper = HOJFullCaseJudgementEntityMapper.INSTANCE;

    @Override
    public Optional<HOJFullCaseJudgement> findBySubmitId(Long submitId) {
        return repository.findByRunId(submitId).map(mapper::toDomain);
    }

    @Override
    public HOJFullCaseJudgement save(HOJFullCaseJudgement judgement) {
        // 如果ID是0，则设置为null确保是新增而不是更新
        if (judgement.getId() != null && judgement.getId() == 0) {
            judgement.setId(null);
        }
        
        HOJFullCaseJudgementEntity entity = mapper.fromDomain(judgement);
        return mapper.toDomain(repository.save(entity));
    }
    
    /**
     * 分页查询用户的提交记录
     */
    public Page<HOJFullCaseJudgement> findSubmissions(
            Long submitterId, 
            String statusStr, 
            String language, 
            int page, 
            int size, 
            String sortBy, 
            String sortDirection) {
        
        // 转换状态字符串为枚举类型
        HOJJudgementStatus status = null;
        if (StringUtils.hasText(statusStr)) {
            try {
                status = HOJJudgementStatus.valueOf(statusStr.toUpperCase());
            } catch (IllegalArgumentException e) {
                // 如果状态名称无效，忽略状态筛选条件
            }
        }
        
        // 创建排序对象
        Sort sort = Sort.unsorted();
        if (StringUtils.hasText(sortBy)) {
            Sort.Direction direction = "desc".equalsIgnoreCase(sortDirection) 
                ? Sort.Direction.DESC 
                : Sort.Direction.ASC;
            sort = Sort.by(direction, sortBy);
        } else {
            // 默认按创建时间倒序排序
            sort = Sort.by(Sort.Direction.DESC, "createdAt");
        }
        
        // 创建分页对象
        Pageable pageable = PageRequest.of(page, size, sort);
        
        // 根据条件进行查询
        Page<HOJFullCaseJudgementEntity> entityPage;
        if (status != null && StringUtils.hasText(language)) {
            entityPage = repository.findBySubmitterIdAndStatusAndLanguage(submitterId, status, language, pageable);
        } else if (status != null) {
            entityPage = repository.findBySubmitterIdAndStatus(submitterId, status, pageable);
        } else if (StringUtils.hasText(language)) {
            entityPage = repository.findBySubmitterIdAndLanguage(submitterId, language, pageable);
        } else {
            entityPage = repository.findBySubmitterId(submitterId, pageable);
        }
        
        // 转换结果
        return entityPage.map(mapper::toDomain);
    }
} 